package com.hs.framework.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.annotation.Nullable
import com.hs.framework.databinding.ViewPaginationBinding
import com.hs.framework.toast.TipsToast
import kotlin.math.ceil

/**
 * 分页-组件
 */
class PaginationView : LinearLayout {

    // 总共多少条
    private var total : Int? = 0
    // 当前页面
    private var currentPage : Int? = 0

    /**
     * 最大页数
     */
    private var maxPage:Int? = 0
    /**
     * 最小页数
     */
    private var minPage:Int = 1

    /**
     * 一页返回的数据
     */
    private var pageSize : Int? = 10

    fun setTotal(total : Int){
        this.total  = total
        this.maxPage =  ceil(total!!.div(pageSize!!.toDouble())).toInt()
        if (total>0)
        {
            minPage = 1
            if (currentPage == 0) {
                currentPage = 1
                mbinding?.jumpToPageEd?.setText("${currentPage}")
            }
        }
        else {
            minPage = 0
        }

        mbinding?.totalTv?.text = "共 ${total} 条"
    }

    private  var  mbinding : ViewPaginationBinding? = null
    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, @Nullable attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init(context, attrs)
    }

    private fun init(context: Context, @Nullable attrs: AttributeSet?) {
        mbinding = ViewPaginationBinding.inflate(LayoutInflater.from(context),this,true)
        setTotal(0)
        initListener()
    }

    private fun initListener() {
        mbinding?.jumpToPrevBtn?.setOnClickListener{
            if (currentPage!! - 1 < minPage!!){
                if (currentPage == minPage) return@setOnClickListener
                currentPage = minPage
            }else{
                currentPage =  currentPage!!.minus(1)
            }
            println("上一页：${currentPage}")
            mbinding?.jumpToPageEd?.setText("${currentPage}")
            callback?.onChange(currentPage!!)
        }
        mbinding?.jumpToNextBtn?.setOnClickListener{
            if (currentPage!!.plus(1) > maxPage!!){
                if (currentPage == maxPage) return@setOnClickListener
                currentPage = maxPage
            }else{
                currentPage = currentPage!!.plus(1)
            }
            println("下一页：${currentPage}")
            mbinding?.jumpToPageEd?.setText("${currentPage}")
            callback?.onChange(currentPage!!)
        }
        mbinding?.jumpToStartBtn?.setOnClickListener{
            if (currentPage == minPage) return@setOnClickListener
            currentPage  = minPage
            println("开始页：${currentPage}")
            mbinding?.jumpToPageEd?.setText("${currentPage}")
            callback?.onChange(currentPage!!)
        }
        mbinding?.jumpToEndBtn?.setOnClickListener{
            // 向上取整数
            if (currentPage == maxPage) return@setOnClickListener
            currentPage =maxPage
            println("结束页：${currentPage}")
            mbinding?.jumpToPageEd?.setText("${currentPage}")
            callback?.onChange(currentPage!!)
        }
        mbinding?.goBtn?.setOnClickListener{
            var pageNum = mbinding!!.jumpToPageEd.text.toString()
            if (pageNum.isBlank()){
                TipsToast.showTips("请输入跳转的页数")
                return@setOnClickListener
            }
            if (currentPage!! >maxPage!!){
                TipsToast.showTips("最大页数是${maxPage}")
                return@setOnClickListener
            }
           var curPage = pageNum.toIntOrNull()
            if (currentPage == curPage) return@setOnClickListener
            // 回调
            println("跳转页：${currentPage}")
            mbinding?.jumpToPageEd?.setText("${currentPage}")
            callback?.onChange(currentPage!!)

//            callback.takeIf {  callback.onChange(currentPage) }
        }
    }

    public fun destroyView(){
        mbinding = null
    }

    private var callback : PaginationViewChangeCallback?=null

    public fun setPaginationViewChangeCallback(callback : PaginationViewChangeCallback){
        this.callback = callback
    }

    interface PaginationViewChangeCallback {
        fun onChange(index: Int)
    }
}